package com.shangxia.leetcode.hoot100.easy.minStack;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Stack;

/**
 * @ProjectName: leetcode
 * @ClassName: MinStack
 * @Time: 2020/11/29
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class MinStack {

    private Stack<Integer> xMinStack;
    private Stack<Integer> minStack;

    public MinStack() {
        xMinStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        xMinStack.push(x);
        if (ObjectUtils.isEmpty(minStack)) {
            minStack.push(x);
        } else {
            if (x <= minStack.peek()) {
                minStack.push(x);
            }
        }
    }

    public void pop() {
        if (!ObjectUtils.isEmpty(minStack) && xMinStack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        xMinStack.pop();
    }

    public int top() {
        return xMinStack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        minStack.top();
        System.out.println(minStack.getMin());
    }
}
